﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using StoreManagementSystem.DomainObjects;
using System.Data;

namespace StoreManagementSystem.DataAccessLayer
{

    public class SQLBrandDAO : IDataAccessObject<StoreManagementSystem.DomainObjects.Entity>
    {
        private ISQLDataProvider DataProvider { get; set; }
        public SQLBrandDAO(ISQLDataProvider SQLDataProvider) 
        {
            DataProvider = SQLDataProvider;
        }


        public void create(DomainObjects.Entity e)
        {
            if (e is Brand)
            {
                Brand brand = (Brand) e;
                List<SQLParam> dbparams = new List<SQLParam>();
                dbparams.Add(new SQLParam("?id", brand.id));
                dbparams.Add(new SQLParam("?Name", brand.Name));

                // build the parameter string to pass in values
                StringBuilder paramstr = new StringBuilder();
                for (int i = 0; i < dbparams.Count - 1; i++)
                {
                    paramstr.Append(dbparams[i].Name + ",");
                }
                paramstr.Append(dbparams[dbparams.Count - 1].Name);

                string SQL = "INSERT INTO brand (id,Name) " +
                        "VALUES(" + paramstr.ToString() + ");";
                DataProvider.Open();
                DataProvider.ExecuteSQL(SQL, dbparams, false);
                DataProvider.Close();
            }
        }

        public DomainObjects.Entity get(params string[] parameters)
        {
            List<SQLParam> dbparams = new List<SQLParam>();
            string SQL;
            string Name = "";
            Name = parameters[0];
            dbparams.Add(new SQLParam("?Name", Name));

            SQL = "SELECT * FROM brand WHERE Name=?Name";
            DataProvider.Open();
            DataTable dt = DataProvider.ExecuteSQL(SQL, dbparams, true);
            DataProvider.Close();
            Brand brand = new Brand();
            brand.id = dt.Rows[0]["id"].ToString();
            brand.Name = dt.Rows[0]["Name"].ToString();
            return brand;

        }

        public void remove(DomainObjects.Entity e)
        {
            throw new NotImplementedException();
        }

        public void update(DomainObjects.Entity e)
        {
            throw new NotImplementedException();
        }
    }
}